package juc;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author lzy
 * Create DateTime: 2018/7/17 14:06
 * @version v1.0
 * <p>
 * 1. ReadWriteLock: 读写锁
 * <p>
 * 写写/读写/写读 需要互斥
 * 读读 不需要互斥
 */
public class TestReadWriteLock {
    public static void main(String[] args) {
        ReadWriteLockDemo rw = new ReadWriteLockDemo();

        new Thread(() -> rw.set(108), "Write").start();

        for (int i = 0; i < 100; i++) {
            new Thread(rw::get).start();
        }
    }
}

class ReadWriteLockDemo {
    private int number = 0;

    private ReadWriteLock lock = new ReentrantReadWriteLock();

    // 读
    public void get() {
        // 上锁
        lock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + ":" + number);
        } finally {
            // 释放锁
            lock.readLock().unlock();
        }
    }

    // 写
    public void set(int number) {
        lock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName());
            this.number = number;
        } finally {
            lock.writeLock().unlock();
        }
    }
}